package com.partners.configure;

import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author liangyongle
 * @date 2018/8/28 0028
 */
@Getter
@Setter
@Component
@ConfigurationProperties(prefix="qiniu")
public class QiniuConfig {

    private String accessKey;

    private String secretKey;

    private String domain;

    private String httpsDomain;

    private String bucket;

    private String project;

    private UploadManager uploadManager;

    private Auth auth;

    private String uploadToken;

    private BucketManager bucketManager;

    @PostConstruct
    private void initUploadComponent() {
        Configuration cfg = new Configuration(Zone.zone0());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth,cfg);
        String upToken = auth.uploadToken(bucket);
        this.uploadManager = uploadManager;
        this.auth = auth;
        this.uploadToken = upToken;
        this.bucketManager = bucketManager;
    }
}
